//
//  ForgetPassViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/9/1.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "ForgetPassViewController.h"
#define LeftMargin 20
#define LeftWidth 80
#define SendWidth 80
#define SendHeight 40
#define TextFieldHeight 44
#define TotalHeight 275
#define ImageWidth 15
#define ImageHeight 20
#define TopMargin 10
#define TopHeight 11
//按钮的tag
typedef enum {
    SendTag = 3001,
    RegisterTag
} Tag;
@interface ForgetPassViewController ()<UITextFieldDelegate>
{
    UIView *_bgView;
    UITextField *_phoneTextFiled;
    UITextField *_codeTextFiled;
    UITextField *_passTextField;
    UITextField *_sureTextField;
    UIButton *_sendCodeBtn;
    UIButton *_registerBtn;
}
@end


@implementation ForgetPassViewController

#pragma mark - 加载视图
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createTitleViewWith:@"找回密码"];
    //创建视图
    [self bulidLayout];
}

#pragma mark - 创建视图
- (void)bulidLayout
{
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
    [self.view addSubview:_bgView];
    //创建发送验证码按钮
    _sendCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_sendCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sendCodeBtn.backgroundColor = SameColor;
    _sendCodeBtn.layer.cornerRadius = 5;
    _sendCodeBtn.layer.masksToBounds = YES;
    _sendCodeBtn.tag = SendTag;
    [_sendCodeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    _sendCodeBtn.titleLabel.font = FontSize(14);
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, TextFieldHeight*4)];
    topView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:topView];
    
    //创建文本框
    NSArray *imagearr = @[@"login1",@"login2",@"login4",@"login5"];
    NSArray *tipsArr = @[@"请输入您的手机号",@"请输入6位数字的验证码",@"请输入您的密码",@"请再次确认密码"];
    for (NSInteger i = 0; i < 4; i++) {
        UIImageView *oneImage = [self createImageView:imagearr[i]];
        oneImage.contentMode = UIViewContentModeScaleAspectFit;
        oneImage.frame = CGRectMake(LeftMargin, TopHeight+TextFieldHeight*i, ImageWidth, ImageHeight);
        [topView addSubview:oneImage];
        
        
        UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(oneImage.frame) + LeftMargin, TextFieldHeight*i, CGRectGetWidth(topView.frame) - LeftMargin * 3 - ImageWidth, TextFieldHeight)];
        textFiled.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
        textFiled.textAlignment = NSTextAlignmentLeft;
        textFiled.backgroundColor = [UIColor clearColor];
        textFiled.delegate = self;
        textFiled.placeholder = tipsArr[i];
        textFiled.font = FontSize(15);
        textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        [topView addSubview:textFiled];
        switch (i) {
            case 0:
            {
                
                CGRect codeFrame = textFiled.frame;
                codeFrame.size.width = CGRectGetWidth(topView.frame) - LeftMargin * 3 - ImageWidth - SendWidth;
                textFiled.frame = codeFrame;
                _phoneTextFiled = textFiled;
                _sendCodeBtn.frame = CGRectMake(CGRectGetMaxX(textFiled.frame), 8, SendWidth, SendHeight-16);
                [topView addSubview:_sendCodeBtn];
            }
                break;
            case 1:
            {
                _codeTextFiled = textFiled;
                
            }
                break;
            case 2:
            {
                textFiled.secureTextEntry = YES;
                _passTextField = textFiled;
            }
                break;
            case 3:
            {
                textFiled.secureTextEntry = YES;
                _sureTextField = textFiled;
            }
                break;
            default:
                break;
        }
        if (i < tipsArr.count - 1) {
            UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, (TextFieldHeight - 1)*(i+1), ScreenSize.width - LeftMargin*2, 1)];
            line.backgroundColor = HexColor(@"e4e4e4");
            [topView addSubview:line];
        }
    }
    
    
    //创建下一步按钮
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registerBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _registerBtn.frame = CGRectMake(LeftMargin, CGRectGetMaxY(topView.frame) + LeftMargin, ScreenSize.width - LeftMargin*2, TextFieldHeight);
    _registerBtn.backgroundColor =  SameColor;
    _registerBtn.layer.cornerRadius = 5;
    _registerBtn.layer.masksToBounds = YES;
    [_registerBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    _registerBtn.tag = RegisterTag;
    _registerBtn.titleLabel.font = FontSize(15);
    [_bgView addSubview:_registerBtn];
    
}
#pragma mark - 按钮事件
- (void)btnClick:(UIButton *)btn
{
    switch (btn.tag ) {
        case SendTag:
        {
            [self sendCode];
        }
            break;
        case RegisterTag:
        {
            [self commitUserPass];
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - 发送验证码请求
- (void)sendCode
{
    if ([CJJHelper trimmingWhitespace:_phoneTextFiled.text].length == 0) {
        ShowMessage(@"请输入手机号");
        return;
    }
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpCode];
    [[HttpRequest sharedInstance] POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0) {
                NSString *vericode = dict[@"vericode"];
                [DearLoadingView showTips:vericode];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView showTips:error];
    }];
}

#pragma mark - 注册用户
- (void)commitUserPass
{
    NSString *userName = [_phoneTextFiled.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *code = [_codeTextFiled.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSString *password = [_passTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *againPass = [_sureTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (userName.length == 0 ) {
        ShowMessage(@"手机号不能为空");
        return;
    }
    if (code.length == 0 ) {
        ShowMessage(@"验证码不能为空");
        return;
    }
    if (password.length == 0 ) {
        ShowMessage(@"密码不能为空");
        return;
    }
    if (againPass.length == 0 ) {
        ShowMessage(@"确认密码不能为空");
        return;
    }
    if (![password isEqualToString:againPass]) {
        ShowMessage(@"两次密码不一致请重新输入");
        return;
    }
    NSDictionary *dict = @{@"userName":userName,@"passWord":password};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpForgetSetPas];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0) {
                [DearLoadingView showTips:dict[@"Message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];
    
    
}


#pragma mark -创建底部的imageView
- (UIImageView *)createImageView:(NSString *)name
{
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:name];
    return imageView;
}
#pragma mark -文本框聚焦的时候
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == _passTextField || textField == _sureTextField ) {
//        [self moveToUp];
    }
}
#pragma mark - 向上移动
- (void)moveToUp{
    
    CGFloat offsetHeight = ScreenSize.height - 64 - (KeyBoard + TotalHeight);
    if (offsetHeight < 0) {
        //键盘要向上弹起
        [UIView animateWithDuration:0.25 animations:^{
            CGRect bgFrame = _bgView.frame;
            bgFrame.origin.y = offsetHeight;
            _bgView.frame = bgFrame;
            
        }];
    }
}

//- (void)textFieldDidEndEditing:(UITextField *)textField{
//    //回到原始位置
//    if (textField == _passTextField || textField == _sureTextField) {
//        [UIView animateWithDuration:0.25 animations:^{
//            CGRect bgFrame = _bgView.frame;
//            bgFrame.origin.y = 0;
//            _bgView.frame = bgFrame;;
//        }];
//    }
//    
//}


#pragma mark - 点击屏幕收键盘
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self hideKeyBoard];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hideKeyBoard];
    return YES;
}
- (void)hideKeyBoard;
{
    [_passTextField resignFirstResponder];
    [_phoneTextFiled resignFirstResponder];
    [_codeTextFiled resignFirstResponder];
    [_sureTextField resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

@end
